package com.ttl.springboot.exception;

import cn.dev33.satoken.exception.SaTokenException;
import com.ttl.springboot.common.Result;
import com.ttl.springboot.common.enmus.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 功能
 * 作者:江哥
 * 日期:2024/8/1 20:10
 */
@ControllerAdvice(basePackages = "com.ttl.springboot.controller")
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(SaTokenException.class)
    @ResponseBody//返回json串
    public Result saTokenError(HttpServletRequest request, SaTokenException e) {
        log.error("业务异常：", e);
        return Result.error(ResultCodeEnum.TOKEN_INVALID_ERROR.code, ResultCodeEnum.TOKEN_INVALID_ERROR.msg);
    }

    @ExceptionHandler(CustomException.class)
    @ResponseBody//返回json串
    public Result customError(HttpServletRequest request, CustomException e) {
        log.error("业务异常：", e);
        return Result.error(e.getCode(), e.getMsg());
    }

    //统一异常处理@ExceptionHandler,主要用于Exception
    @ExceptionHandler(Exception.class)
    @ResponseBody//返回json串
    public Result error(HttpServletRequest request, Exception e) {
        log.error("异常信息：", e);
        return Result.error();
    }
}
